import r from "highlight.js/lib/languages/r";

// 时间按戳毫秒转秒
export function formatSeconds(value) {
  const seconds = Number(value) / 1000;
  return seconds;
}


export function formatDate(date, format) {
  const map = {
    'M': date.getMonth() + 1, // 月份
    'd': date.getDate(), // 日
    'h': date.getHours(), // 小时
    'm': date.getMinutes(), // 分钟
    's': date.getSeconds(), // 秒
    'q': Math.floor((date.getMonth() + 3) / 3), // 季度
    'S': date.getMilliseconds() // 毫秒
  };

  format = format.replace(/([yMdhmsqS])+/g, (all, t) => {
    let v = map[t];
    if (v !== undefined) {
      if (all.length > 1) {
        v = '0' + v;
        v = v.substr(v.length - all.length);
      }
      return v;
    } else if (t === 'y') {
      return (date.getFullYear() + '').substr(4 - all.length);
    }
    return all;
  });
  return format;
}

//转换标准时间格式
export function timeFormat(time) {
  // 时间格式化 2019-09-08
  let year = time.getFullYear();
  let month =
    time.getMonth() + 1 > 10 ?
    time.getMonth() + 1 :
    "0" + (time.getMonth() + 1);
  let day = time.getDate() > 10 ? time.getDate() : "0" + time.getDate();
  return year + "-" + month + "-" + day;
}

// 当前时间后十分钟（时分）
export function afterTenMinutes() {
  let date = new Date();
  let minutes = parseInt("10");
  let interTimes = minutes * 60 * 1000;
  interTimes = parseInt(interTimes);
  date = new Date(Date.parse(date) + interTimes);
  // console.log(interTimes);
  // console.log(minutes);
  // console.log(date);
  let hour = date.getHours();
  minutes = date.getMinutes();
  if (hour >= 0 && hour <= 9) {
    hour = "0" + hour;
  }
  if (minutes >= 0 && minutes <= 9) {
    minutes = "0" + minutes;
  }
  // console.log(hour);
  // console.log(minutes);
  let currentdate = hour + ":" + minutes;
  return currentdate;
}

// 获取最近7,15,30等任意时间的开始和结束时间
export function timeForRange(days) {
  let time1 = new Date();
  time1.setHours(0, 0, 0, 0);
  let time2 = new Date();
  time2.setHours(0, 0, 0, 0);
  time2.setDate(time2.getDate() + days)

  if (time1.getTime() >= time2.getTime()) {
    time2.setHours(0, 0, 0, 0)
    time1.setHours(23, 59, 59, 999)
    return [formatDate(time2, 'yyyy-MM-dd hh:mm:ss'), formatDate(time1, 'yyyy-MM-dd hh:mm:ss')]
  }
  time1.setHours(0, 0, 0, 0)
  time2.setHours(23, 59, 59, 999)
  return [formatDate(time1, 'yyyy-MM-dd hh:mm:ss'), formatDate(time2, 'yyyy-MM-dd hh:mm:ss')]
}
export function dateForRange(days) {
  let time1 = new Date();
  time1.setHours(0, 0, 0, 0);
  let time2 = new Date();
  time2.setHours(0, 0, 0, 0);
  time2.setDate(time2.getDate() + days)

  if (time1.getTime() >= time2.getTime()) {
    time2.setHours(0, 0, 0, 0)
    time1.setHours(23, 59, 59, 999)
    return [formatDate(time2, 'yyyy-MM-dd'), formatDate(time1, 'yyyy-MM-dd')]
  }
  time1.setHours(0, 0, 0, 0)
  time2.setHours(23, 59, 59, 999)
  return [formatDate(time1, 'yyyy-MM-dd'), formatDate(time2, 'yyyy-MM-dd')]
}
// 计算两时间相差天数
export function cancelDays(data) {
  let dateSpan = 0;
  let sDate1 = Date.parse(data.startTime);
  let sDate2 = Date.parse(data.endTime);
  dateSpan = sDate1 - sDate2;
  dateSpan = Math.abs(dateSpan);
  let days = Math.floor(dateSpan / (24 * 3600 * 1000));
  return days;
}

//获取当前时间
export function nowTime() {
  //获取当前时间
  let now = new Date();
  let _month =
    10 > now.getMonth() + 1 ? "0" + (now.getMonth() + 1) : now.getMonth() + 1;
  let _day = 10 > now.getDate() ? "0" + now.getDate() : now.getDate();
  let _hour = 10 > now.getHours() ? "0" + now.getHours() : now.getHours();
  let _minute =
    10 > now.getMinutes() ? "0" + now.getMinutes() : now.getMinutes();
  let _second =
    10 > now.getSeconds() ? "0" + now.getSeconds() : now.getSeconds();
  return (
    now.getFullYear() +
    "-" +
    _month +
    "-" +
    _day +
    " " +
    _hour +
    ":" +
    _minute +
    ":" +
    _second
  );
}

//element ui时间控件快捷选择
export const pickerOptions = [{
    text: "今天",
    onClick(picker) {
      picker.$emit("pick", new Date());
    }
  },
  {
    text: "昨天",
    onClick(picker) {
      const date = new Date();
      date.setTime(date.getTime() - 3600 * 1000 * 24);
      picker.$emit("pick", date);
    }
  },
  {
    text: "一周前",
    onClick(picker) {
      const date = new Date();
      date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
      picker.$emit("pick", date);
    }
  },
  {
    text: "30天前",
    onClick(picker) {
      const date = new Date();
      date.setTime(date.getTime() - 3600 * 1000 * 24 * 30);
      picker.$emit("pick", date);
    }
  }
];

//element ui时间控件禁用大于当前日期
export function disabledDate(time) {
  return (
    time.getTime() >
    new Date(
      new Date().toLocaleDateString().replace(/\//g, "-") + " " + "00:00:00"
    ).getTime()
  );
}
